package com.xinzhidi.web.balancecontrol.service;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xinzhidi.common.constant.AgreementConstants;
import com.xinzhidi.common.core.page.TableDataInfo;
import com.xinzhidi.web.balancecontrol.domain.po.BalanceControlPo;
import com.xinzhidi.web.balancecontrol.domain.po.LockPo;
import com.xinzhidi.web.balancecontrol.domain.vo.ControlVo;
import com.xinzhidi.web.balancecontrol.mapper.BalanceControlMapper;
import com.xinzhidi.web.build.domain.BuildUnit;
import com.xinzhidi.web.build.domain.CBuild;
import com.xinzhidi.web.build.service.IBuildUnitService;
import com.xinzhidi.web.build.service.ICBuildService;
import com.xinzhidi.web.house.domain.Household;
import com.xinzhidi.web.house.service.IHouseholdService;
import com.xinzhidi.web.onlineMonitor.domain.vo.OnlineMonitorVo;
import com.xinzhidi.web.onlineMonitor.mapper.OnlineMonitorMapper;
import com.xinzhidi.web.plot.domain.entity.CPlot;
import com.xinzhidi.web.plot.service.ICPlotService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * @author dell
 */
@Service
@RequiredArgsConstructor
public class BalanceControlService {

    private final BalanceControlMapper controlMapper;

    private final ICPlotService plotService;

    private final ICBuildService buildService;

    private final IBuildUnitService buildUnitService;

    private final IHouseholdService householdService;

    private final OnlineMonitorMapper monitorMapper;

    /**
     * 平衡调控-小区列表
     *
     * @param po
     * @return
     */
    public TableDataInfo<ControlVo> listPlot(BalanceControlPo po) {
        List<ControlVo> list = controlMapper.listPlot(po);
        return TableDataInfo.getDataTable(list);
    }

    /**
     * 平衡调控-楼栋列表
     *
     * @param po
     * @return
     */
    public TableDataInfo<ControlVo> listBuild(BalanceControlPo po) {
        List<ControlVo> list = controlMapper.listBuild(po);
        //获取楼栋指标
        ControlVo controlVo;
        if(CollectionUtils.isNotEmpty(list)){
            for(ControlVo vo : list){
                controlVo = controlMapper.getValue(vo.getBuildId());
                if(controlVo != null){
//                    if(controlVo.getWaterSupplyTemperature() != null){
//                        vo.setWaterSupplyTemperature(controlVo.getWaterSupplyTemperature());
//                    }
//                    if (controlVo.getReturnWaterTemperature() != null){
//                        vo.setReturnWaterTemperature(controlVo.getReturnWaterTemperature());
//                    }
//                    if(controlVo.getValveOpen() != null){
//                        vo.setValveOpen(controlVo.getValveOpen());
//                    }
                }
            }
        }


        return TableDataInfo.getDataTable(list);
    }

    /**
     * 平衡调控-单元列表
     *
     * @param po
     * @return
     */
    public TableDataInfo<ControlVo> listBuildUnit(BalanceControlPo po) {
        List<ControlVo> list = controlMapper.listBuildUnit(po);
        if(CollectionUtils.isNotEmpty(list)){
            OnlineMonitorVo monitorVo;
            for(ControlVo vo : list){
                monitorVo = monitorMapper.getBuildUnitIndex(vo.getUnitId());
                if (monitorVo != null) {
                    vo.setWaterSupplyTemperature(monitorVo.getWaterSupplyTemperature());
                    vo.setReturnWaterTemperature(monitorVo.getReturnWaterTemperature());
                }
            }

        }

        return TableDataInfo.getDataTable(list);
    }

    /**
     * 平衡调控-住户列表
     *
     * @param po
     * @return
     */
    public TableDataInfo<ControlVo> listHousehold(BalanceControlPo po) {
        List<ControlVo> list = controlMapper.listHousehold(po);
        return TableDataInfo.getDataTable(list);
    }

    /**
     * 平衡调控-锁定/解锁
     *
     * @param po
     */
    @Transactional(rollbackFor = Exception.class)
    public void lockOrUnlock(LockPo po) {
        List<Long> ids = Arrays.asList(po.getId());
        if (po.getType().equals(AgreementConstants.DataType.PLOT)) {
            UpdateWrapper<CPlot> plotUpdateWrapper = new UpdateWrapper<>();
            plotUpdateWrapper.set("lock_status", po.getLockStatus());
            plotUpdateWrapper.eq("id", po.getId());
            plotService.update(plotUpdateWrapper);
            //修改该小区下所有楼栋的锁定状态
            LambdaQueryWrapper<CBuild> buildWrapper = Wrappers.lambdaQuery();
            buildWrapper.eq(CBuild::getPlotId, po.getId()).eq(CBuild::getDelFlag, AgreementConstants.DEL_FLAG_NO);
            List<CBuild> buildList = buildService.list(buildWrapper);
            List<Long> buildIds = buildList.stream().map(CBuild::getId).collect(java.util.stream.Collectors.toList());
            if (CollectionUtils.isNotEmpty(buildIds)) {
                this.updateBuildStatus(buildIds, po.getLockStatus());
            }
        } else if (po.getType().equals(AgreementConstants.DataType.BUILD)) {
            this.updateBuildStatus(ids, po.getLockStatus());
        } else if (po.getType().equals(AgreementConstants.DataType.BUILD_UNIT)) {
            this.updateBuildUnitStatus(ids, po.getLockStatus());
        } else if (po.getType().equals(AgreementConstants.DataType.HOUSEHOLD)) {
            this.updateHouseholdStatus(ids, po.getLockStatus());
        }
    }

    /**
     * 修改楼栋锁定状态
     *
     * @param buildIds   楼栋id集合
     * @param lockStatus 锁定状态
     */
    private void updateBuildStatus(List<Long> buildIds, Integer lockStatus) {
        if (CollectionUtils.isNotEmpty(buildIds)) {
            UpdateWrapper<CBuild> buildUpdateWrapper = new UpdateWrapper<>();
            buildUpdateWrapper.set("lock_status", lockStatus);
            buildUpdateWrapper.in("id", buildIds);
            buildService.update(buildUpdateWrapper);
            //修改该楼栋下所有单元的锁定状态
            LambdaQueryWrapper<BuildUnit> buildUnitWrapper = Wrappers.lambdaQuery();
            buildUnitWrapper.in(BuildUnit::getBuildId, buildIds).eq(BuildUnit::getDelFlag, AgreementConstants.DEL_FLAG_NO);
            List<BuildUnit> buildUnitList = buildUnitService.list(buildUnitWrapper);
            List<Long> buildUnitIds = buildUnitList.stream().map(BuildUnit::getId).collect(java.util.stream.Collectors.toList());
            if (CollectionUtils.isNotEmpty(buildUnitIds)) {
                this.updateBuildUnitStatus(buildUnitIds, lockStatus);
            }

        }
    }

    /**
     * 修改单元锁定状态
     *
     * @param buildUnitIds 单元id集合
     * @param lockStatus   锁定状态
     */
    private void updateBuildUnitStatus(List<Long> buildUnitIds, Integer lockStatus) {
        if (CollectionUtils.isNotEmpty(buildUnitIds)) {
            UpdateWrapper<BuildUnit> buildUnitUpdateWrapper = new UpdateWrapper<>();
            buildUnitUpdateWrapper.set("lock_status", lockStatus);
            buildUnitUpdateWrapper.in("id", buildUnitIds);
            buildUnitService.update(buildUnitUpdateWrapper);
            //修改该单元下所有住户的锁定状态
            LambdaQueryWrapper<Household> householdWrapper = Wrappers.lambdaQuery();
            householdWrapper.in(Household::getUnit, buildUnitIds).eq(Household::getDelFlag, AgreementConstants.DEL_FLAG_NO);
            List<Household> householdList = householdService.list(householdWrapper);
            List<Long> householdIds = householdList.stream().map(Household::getId).collect(java.util.stream.Collectors.toList());
            if (CollectionUtils.isNotEmpty(householdIds)) {
                this.updateHouseholdStatus(householdIds, lockStatus);
            }
        }
    }

    /**
     * 修改住户锁定状态
     *
     * @param householdIds 住户id集合
     * @param lockStatus   锁定状态
     */
    private void updateHouseholdStatus(List<Long> householdIds, Integer lockStatus) {
        if (CollectionUtils.isNotEmpty(householdIds)) {
            UpdateWrapper<Household> householdUpdateWrapper = new UpdateWrapper<>();
            householdUpdateWrapper.set("lock_status", lockStatus);
            householdUpdateWrapper.in("id", householdIds);
            householdService.update(householdUpdateWrapper);
        }
    }
}
